package com.leo.tool;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

public final class WordCount {

    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("WordCount").setMaster("local");
        try (JavaSparkContext sparkContext = new JavaSparkContext(sparkConf)) {
            sparkContext.textFile("pom.xml")
                    .flatMap(content -> Arrays.asList(content.split(" ")).iterator())
                    .mapToPair(t -> new Tuple2<>(t, 1))
                    .reduceByKey(Integer::sum)
                    .collect()
                    .forEach(System.out::println);
        }
    }
}